﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
//using System.Data.Entity;
//using System.Data.Objects;

namespace Paltop_.Controllers
{
    public class StikyNoteController
    {
        public string StickyNoteDragged(int xpos, int ypos, int sid)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == sid
                        select S;
            notes.First().XPosition = xpos;
            notes.First().YPosition = ypos;
            pe.SaveChanges();
            return "ok";
           
        }
        public string StickyNoteCreate(bool addKey , int userId)
        {
            try
            {
                string oldNotes = "";
                string returnValue = "";
                
                Paltop_DatabaseLayer.PaltopDatabaseEntities pd = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
                
                if (!addKey)
                {
                    var notes = from SN in pd.StickyNotes
                                where SN.AccountID == userId
                                select SN;
                    if (notes.Count() > 0)
                    {
                        int count = 0;
                        oldNotes = StickyNoteReturnCreated(userId, ref count);
                       
                    }
                    else
                        returnValue = CreateNoteHelper(returnValue, pd, userId);
                }
                else
                {
                    returnValue = CreateNoteHelper(returnValue, pd,userId);
                }
                return returnValue + oldNotes;
            }
            catch (Exception ex)
            {
                return "Error StikyNote create:" + ex.Message;
            }

        }

        private static string CreateNoteHelper(string returnValue, Paltop_DatabaseLayer.PaltopDatabaseEntities pd,int userId)
        {
            Paltop_DatabaseLayer.StickyNote stiky = new Paltop_DatabaseLayer.StickyNote();
            stiky.AccountID = userId;
            stiky.Content = "";
            stiky.SavedDate = DateTime.Today;
            stiky.XPosition = 531;
            stiky.YPosition = 120;
            stiky.Width = 200;
            stiky.Height = 250;
            stiky.Style = "StickyNote1";
            pd.StickyNotes.AddObject(stiky);
            pd.SaveChanges();


            returnValue = "{ \"StickyNoteID\":" + stiky.StickyNoteID + ",\"XPosition\":" + stiky.XPosition + ",\"YPosition\":" + stiky.YPosition + ",\"Width\":" + stiky.Width + ",\"Height\":" + stiky.Height + ",\"Style\":\"" + stiky.Style + "\"}";
            return returnValue;
        }
        public string StickyNoteResized(int width,int height,int stickyId)
        {
            System.Diagnostics.Debug.WriteLine(width + "," + height + "," + stickyId);
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == stickyId
                        select S;
            notes.First().Width = width;
            notes.First().Height = height;
            
            pe.SaveChanges();
            return "ok";
        }
        internal string StickyNoteSave(string content, int stickyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == stickyId
                        select S;
            notes.First().Content = content;
            pe.SaveChanges();
            return "ok";
        }
        internal string StickyNoteDelete(int stickyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == stickyId
                        select S;
            pe.StickyNotes.DeleteObject(notes.First());
            pe.SaveChanges();
            return "ok";
        }
        internal string StickyNoteClose(int stickyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == stickyId
                        select S;
            notes.First().CloseFlag=true;
            pe.SaveChanges();
            return "ok";
        }
        internal string StickyNoteMinimize(int stickyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pe = new PaltopDatabaseEntities();
            var notes = from S in pe.StickyNotes
                        where S.StickyNoteID == stickyId
                        select S;
            notes.First().MinimizeFlag = true;
            pe.SaveChanges();
            return "ok";
        }
        internal string StickyNoteReturnCreated(int userId,ref int  count)
        {
            string html = "";
            string script = "";
            Paltop_DatabaseLayer.PaltopDatabaseEntities pd = new Paltop_DatabaseLayer.PaltopDatabaseEntities();
            var notes = from SN in pd.StickyNotes
                        where SN.AccountID == userId
                        select SN;
            if (notes.Count() > 0)
            {
                count = notes.Count();
                script += "<script type=\"text/javascript\">"
                    + "        $(function () {\n";
                foreach (var item in notes)
                {
                    item.MinimizeFlag = false;
                    script += "$(\"#StickyNote_" + item.StickyNoteID + "\").resizable({"
    + "                animate: true,"
    + "                stop: StopResizing,"
    + "                resize:StickyResizing"
    + "            });\n"
    + "            $(\"#StickyNote_" + item.StickyNoteID + "\").draggable("
    + "            {"
    + "                stop: StopDragging"
    + "            });";
                    script += " $('#StickyNote_" + item.StickyNoteID + " .Content').bind(\"contextmenu\", CreateStickyMenu);";
                    html += " <div id=\"StickyNote_" + item.StickyNoteID + "\" class=\"" + item.Style + " StickyNote ui-resizable\" style=\"left: " +
                        item.XPosition + "px; top: " + item.YPosition + "px; position: absolute; width: " + item.Width + "px;  height: " + item.Height +"px;\">"
        + "            <div class=\"StickyNoteHeader\">"
        + "                <a href=\"#\">"
        + "                    <img id=\"stickyNoteAddIcon\" src=\"../../TemplateImages/stickyNoteAddIcon.png\" alt=\"\" onclick=\"StickyNoteAdd()\"/></a>"
        + "                <span style=\"width: 80%;\"></span><a href=\"#\" style=\"float: right;\">"
        + "                    <img id=\"stickyNoteCloseIcon\" src=\"../../TemplateImages/stickyNoteCloseIcon.png\""
        + "                        alt=\"\" onclick=\"StickyNoteDelete(" + item.StickyNoteID + ")\" /></a></div><div id=\"StickyContent_"+item.StickyNoteID+"\" class=\"Content\" >"
        + "            <textarea class=\"StickyNoteTextarea\" rows=\"10\" cols=\"20\"  onchange=\"StickyTextChanged()\" onblur=\"StickyNoteSave(" + item.StickyNoteID + ",this)\">" + item.Content + "</textarea></div>"
        + "        </div>\n";
                }
                pd.SaveChanges();
                script += "        });\n"
    + " </script>\n";
            }
            return script + html; 
        }

        internal string StickyNoteMinMax(int userId,string minMaxStatus)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pd = new PaltopDatabaseEntities();
            var notes = from S in pd.StickyNotes
                        where S.AccountID == userId
                        select S;
            string retValue = "";
            foreach (var item in notes)
            {
                if (minMaxStatus == "min")
                    item.MinimizeFlag = true;
                else
                    item.MinimizeFlag = false;
                retValue += item.StickyNoteID + ",";
            }
            pd.SaveChanges();
            return retValue.Substring(0, retValue.Length - 1);
        }

        internal string StickyChangeStyle(int userId, int stikyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pd = new PaltopDatabaseEntities();
            var notes = from S in pd.StickyNotes
                        where S.AccountID == userId && S.StickyNoteID==stikyId
                        select S;
            if (notes.First().Style == "StickyNote1")
            {
                notes.First().Style = "StickyNote2";
                pd.SaveChanges();
                return "StickyNote1$StickyNote2";
            }
            else
            {
                notes.First().Style = "StickyNote1";
                pd.SaveChanges();
                return "StickyNote2$StickyNote1";
            }
        }

        internal string StickyNoteMin(int userId, int stikyId)
        {
            Paltop_DatabaseLayer.PaltopDatabaseEntities pd = new PaltopDatabaseEntities();
            var notes = from S in pd.StickyNotes
                        where S.AccountID == userId && S.StickyNoteID == stikyId
                        select S;
            notes.First().MinimizeFlag = true;
            pd.SaveChanges();
            return "ok";
        }
    }
}
